package com.cy.mall.service.goods.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cy.mall.dao.mapper.goods.GoodsImageMapper;
import com.cy.mall.dao.mapper.goods.GoodsMapper;
import com.cy.mall.model.PageBean;
import com.cy.mall.model.enums.goods.GoodsImageType;
import com.cy.mall.model.query.goods.GoodsListQuery;
import com.cy.mall.model.vo.goods.GoodsDetailVO;
import com.cy.mall.model.vo.goods.GoodsImageListVO;
import com.cy.mall.model.vo.goods.GoodsListVO;
import com.cy.mall.model.vo.product.ProductRateVO;
import com.cy.mall.service.BaseService;
import com.cy.mall.service.goods.GoodsService;

/**
 * 商品服务GoodsService
 *
 * @author tanyilin
 *
 * @create 2019年1月24日 下午5:24:42
 */
@Service
public class GoodsServiceImpl extends BaseService implements GoodsService {

	@Autowired
	private GoodsMapper goodsMapper;
	@Autowired
	private GoodsImageMapper goodsImageMapper;

	@Override
	public PageBean<GoodsListVO> selectGoodsList(GoodsListQuery query) {
		int total = goodsMapper.selectGoodsListCount(query);
		List<GoodsListVO> list = new ArrayList<>();
		if(total > 0) {
			query.startPage();
			list = goodsMapper.selectGoodsList(query);
		}
		
		return new PageBean<>(list, total);
	}

	@Override
	public GoodsDetailVO selectGoodsDetail(Long goodsId) {
		
		//查询商品信息
		GoodsDetailVO goodsDetailVO = goodsMapper.selectGoodsDetail(goodsId);
		state(goodsDetailVO != null, "商品数据异常");
		
		//查询对应贷款产品利率数组
		List<ProductRateVO> productRateList = goodsMapper.selectProductRateList(goodsDetailVO.getProductId());
		
		//查询商品详情顶部banner滚动图
		List<GoodsImageListVO> topBannerImageList = goodsImageMapper.selectGoodsImageListByGoodsIdAndType(goodsId, GoodsImageType.DETAIL_TOP_BANNER_IMAGE);
		//查询商品详情底部图
		List<GoodsImageListVO> bottomImageList = goodsImageMapper.selectGoodsImageListByGoodsIdAndType(goodsId, GoodsImageType.DETAIL_BOTTOM_IMAGE);
		
		goodsDetailVO.setProductRateList(productRateList);
		goodsDetailVO.setTopBannerImageList(topBannerImageList);
		goodsDetailVO.setBottomImageList(bottomImageList);
		
		return goodsDetailVO;
	}

	@Override
	public List<ProductRateVO> selectProductRateList(Long productId) {
		//查询对应贷款产品利率数组
		List<ProductRateVO> productRateList = goodsMapper.selectProductRateList(productId);
		return productRateList;
	}
	
	
}
